package com.Socket.lianxi.homework2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class HwReceiver {
    public static void main(String[] args) throws Exception{
        DatagramSocket datagramSocket = new DatagramSocket(8888);

        byte[] bytes = new byte[1024];
        DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
        datagramSocket.receive(datagramPacket);
        int length = datagramPacket.getLength();
        byte[] data = datagramPacket.getData();
        String s = new String(data,0,length);
        System.out.println(s);
        String answer = "";
        if ("四大名著是那些".equals(s)){
            answer = "四大名著：西游记，红楼梦，水浒传，三国演义";
        }else {
            answer = "你说的啥？";
        }

        data = answer.getBytes();
        DatagramPacket datagramPacket1 = new DatagramPacket(data, data.length, InetAddress.getByName("192.168.2.101"), 9999);
        datagramSocket.send(datagramPacket1);
        datagramSocket.close();
    }
}
